package com.wd.health.utils;

import android.content.Context;

import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

/**
 * @ProjectName:shopmall
 * @Package:com.wd.health.utils
 * @ClassName:WeChatPay
 * @UpdateUser: 裴童童
 * @date: 2020/12/21
 */
public class WeChatPay {
    /**
     * 当前项目在微信开放平台审核通过的appID
     */
    public static final String WX_APP_ID = "wxe3fcbe8a55cd33ff";

    private static IWXAPI iwxapi;
    private static Context context;

    /**
     * 注册(微信)
     */
    public static void regToWX(Context context) {
        WeChatPay.context = context;
        iwxapi = WXAPIFactory.createWXAPI(context, WX_APP_ID, true);
        iwxapi.registerApp(WX_APP_ID);
    }


    /**
     * 微信支付
     * @param
     */
    public static boolean getWXIshar(String appId,
                                     String partnerId,
                                     String prepayId,
                                     String nonceStr,
                                     String timeStamp,
                                     String sign) {

        /*System.out.println("---------appId" + appId);
        System.out.println("---------partnerId" + partnerId);
        System.out.println("---------prepayId" + prepayId);
        System.out.println("---------nonceStr" + nonceStr);
        System.out.println("---------timeStamp" + timeStamp);
        System.out.println("---------sign" + sign);*/
        System.out.println("getWXIshar" + appId + " " + partnerId + " " + prepayId + " " + nonceStr + " " + timeStamp + " " + sign);
        if (iwxapi == null) {
            iwxapi = WXAPIFactory.createWXAPI(context, WX_APP_ID, true);
        }
        PayReq request = new PayReq();
        request.appId = WX_APP_ID;
        request.partnerId = partnerId;// 微信支付分配的商户号
        request.prepayId = prepayId;// 预支付订单号，app服务器调用“统一下单”接口获取
        request.packageValue = "Sign=WXPay";// 固定值Sign=WXPay，可以直接写死，服务器返回的也是这个固定值
        request.nonceStr = nonceStr;// 随机字符串，不长于32位
        request.timeStamp = timeStamp;// 时间戳
        request.sign = sign;// 签名，
        return iwxapi.sendReq(request);
    }

} 
